/*============================================================================*/
/*                        Tortoise Team			                              */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* C Source:         %APP_FrontWheels.c%
* Instance:         RPL_1
* %version:         1.1 %
* %created_by:      Andres Torres Garcia %
* %date_created:    Sunday May 20 13:41:01 2012 %
*=============================================================================*/
/* DESCRIPTION : C source file for the front wheels                           */
/*============================================================================*/
/* FUNCTION COMMENT : Contains the definitions of the functions declared in	  */	
/*	APP_FrontWheels.h		                                                  */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 26/05/2012  |                               | Andres Torres    */
/*  1.1      | 10/08/2012 |                               | Andres Torres	  */
/* Integration under Continuus CM                                             */
/*============================================================================*/

/* Includes */
/* -------- */
#include "stdtypedef.h"
#include "HAL_ServoMotor.h"
#include "APP_FrontWheels.h"

/* Functions macros, constants, types and datas         */
/* ---------------------------------------------------- */

/*==================================================*/ 
/* Definition of constants                          */
/*==================================================*/ 


/*======================================================*/ 
/* Definition of RAM variables                          */
/*======================================================*/ 
/* BYTE RAM variables */

/** \brief Current percentage direction of the servo motor */
static T_SWORD sb_CurrPercDirec =	MED_VALUE;

/*======================================================*/ 
/* close variable declaration sections                  */
/*======================================================*/ 

/* Private defines */

/*! \def INC_DEC                                                                                  
  Rate of increment and decrement.                                                                                   
 */
#define INC_DEC			10

/* Private functions prototypes */
/* ---------------------------- */

/* Exported functions prototypes */
/* ----------------------------- */
void vfnCalibrate_FrontWheels(void);
void vfnInit_FrontWheels(void);
void vfnSetPos_FrontWheels(T_UWORD ub_degreesPos);
T_SWORD vfnLeftDirection(void);
T_SWORD vfnRightDirection(void);
T_SWORD vfnGetPos_FrontWheels(void);
/* Inline functions */
/* ---------------- */


/* Private functions */
/* ----------------- */


/* Exported functions */
/* ------------------ */

/**************************************************************
 *  Name                 :	vfnCalibrate_FrontWheels.
 *  Description          :	Calibrate the front wheels of the car.
 *  Parameters           :  [Input, Output, Input / output]
 *  Return               :	void
 *  Critical/explanation :    No
 **************************************************************/
///////////////////////////////////////////////////////////////////////
/// \brief Calibrate the front wheels of the car.
/////////////////////////////////////////////////////////////////////////
void vfnCalibrate_FrontWheels(void)
{
	vfnCalibrate_ServoMotor();
}

/**************************************************************
 *  Name                 :	vfnInit_FrontWheels
 *  Description          :	Initialize the front wheels
 *  Parameters           :  [Input, Output, Input / output]
 *  Return               :	void
 *  Critical/explanation :    No
 **************************************************************/
///////////////////////////////////////////////////////////////////////
/// \brief Initialize the front wheels
/////////////////////////////////////////////////////////////////////////
void vfnInit_FrontWheels(void)
{
	vfnInit_ServoMotor();
	vfnSet_ServoMotor(MED_VALUE, MIN_VALUE, MAX_VALUE);
}

/**************************************************************
 *  Name                 :	vfnSetPos_FrontWheels
 *  Description          :	Set the front wheel position
 *  Parameters           :  [Input: Set the position depending of your selected range, Output, Input / output]
 *  Return               :
 *  Critical/explanation :    No
 **************************************************************/
///////////////////////////////////////////////////////////////////////
/// \brief Set the front wheel position
/// \param ub_degreesPos Store the desired position of the front wheels
/////////////////////////////////////////////////////////////////////////
void vfnSetPos_FrontWheels(T_UWORD ub_degreesPos)
{
	vfnSet_ServoMotor(ub_degreesPos, MIN_VALUE, MAX_VALUE);
}

/**************************************************************
 *  Name                 :	vfnGetPos_FrontWheels
 *  Description          :	Get the current position of the front wheels
 *  Parameters           :  [Input, Output, Input / output]
 *  Return               :	T_SWORD: current position.
 *  Critical/explanation :    No
 **************************************************************/
///////////////////////////////////////////////////////////////////////
/// \brief Get the current position of the front wheels
/// \return The position of the current front wheel.
/////////////////////////////////////////////////////////////////////////
T_SWORD vfnGetPos_FrontWheels(void)
{
	return sb_CurrPercDirec;
}

/**************************************************************
 *  Name                 :	vfnLeftDirection
 *  Description          :	Move a degree the front wheels to the left.
 *  Parameters           :  [Input, Output, Input / output]
 *  Return               :	void
 *  Critical/explanation :    No
 **************************************************************/
///////////////////////////////////////////////////////////////////////
/// \brief Move a degree the front wheels to the left.
/////////////////////////////////////////////////////////////////////////
 T_SWORD vfnLeftDirection(void)
 {
 	sb_CurrPercDirec -= INC_DEC;
 	
 	if(sb_CurrPercDirec < MIN_VALUE)
 	{
 		sb_CurrPercDirec = MIN_VALUE;
 	}
 
 	vfnSet_ServoMotor(sb_CurrPercDirec, MIN_VALUE, MAX_VALUE);
 	
 	return sb_CurrPercDirec;
 }
 
 /**************************************************************
 *  Name                 :	vfnInit_FrontWheels
 *  Description          :	Move a degree the front wheels to the right.
 *  Parameters           :  [Input, Output, Input / output]
 *  Return               :	void
 *  Critical/explanation :    No
 **************************************************************/
///////////////////////////////////////////////////////////////////////
/// \brief Move a degree the front wheels to the right.
/////////////////////////////////////////////////////////////////////////
 T_SWORD vfnRightDirection(void)
 {
 	sb_CurrPercDirec += INC_DEC;
 	
 	if(sb_CurrPercDirec > MAX_VALUE)
 	{
 		sb_CurrPercDirec = MAX_VALUE;
 	}
 	
 	vfnSet_ServoMotor(sb_CurrPercDirec, MIN_VALUE, MAX_VALUE);
 	
 	return sb_CurrPercDirec;
 }